//
//  LCNavigationViewController.m
//  LCPackage
//
//  Created by 马军凯 on 2021/6/22.
//  Copyright © 2021 龙采科技集团. All rights reserved.
//

#import "LCNavigationViewController.h"

@interface LCNavigationViewController ()
<UIGestureRecognizerDelegate, UINavigationControllerDelegate>

@end

@implementation LCNavigationViewController

+ (void)initialize {
    UINavigationBar *bar = [UINavigationBar appearance];
    CGRect rect = CGRectMake(0.0f, 0.0f, SCREEN_WIDTH, 0.5);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [UIColor LC_hex:@"#FAFAFA"].CGColor);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    [bar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    [bar setShadowImage:image];
    
    bar.translucent  = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //如果滑动移除控制器的功能失效，清空代理（让控制器重新设置这个功能）
//    self.interactivePopGestureRecognizer.delegate = nil;
    //通过自定义手势取消 系统自己的POP手势  得到系统的pop方法
    UIGestureRecognizer *gesture = self.interactivePopGestureRecognizer;
    //然后把他的可动能力关闭
    gesture.enabled = NO;
    //得到gesture手势的view
    UIView *gestureView = gesture.view;
    
    //重新定义一个手势动作 (pan)
    _panges = [[UIPanGestureRecognizer alloc] init];
    //签订手势的协议
    _panges.delegate = self;
    //限制手指的根数
    _panges.maximumNumberOfTouches = 1;
    //然后把新的手势动作加到手势view上
    [gestureView addGestureRecognizer:_panges];
    
    //获取系统手势的target数组
    NSMutableArray *_targets = [gesture valueForKey:@"_targets"];
    //获取它的唯一对象，我们知道它是一个叫UIGestureRecognizerTarget的私有类，它有一个属性叫_target
    id gestureRecognizerTarget = [_targets firstObject];
    //获取_target:_UINavigationInteractiveTransition，它有一个方法叫handleNavigationTransition:
    id navigationInteractiveTransition = [gestureRecognizerTarget valueForKey:@"_target"];
    //通过前面的打印，我们从控制台获取出来它的方法签名。
    SEL handleTransition = NSSelectorFromString(@"handleNavigationTransition:");
    //创建一个与系统一模一样的手势，我们只把它的类改为UIPanGestureRecognizer
    [_panges addTarget:navigationInteractiveTransition action:handleTransition];
}

- (BOOL)gestureRecognizerShouldBegin:(UIPanGestureRecognizer *)gestureRecognizer {
    /**
     *  这里有两个条件不允许手势执行，1、当前控制器为根控制器；2、如果这个push、pop动画正在执行（私有属性）
     */
    //判断是否动画正在进行,如果在执行,没有任何效应
    if ([gestureRecognizer translationInView:gestureRecognizer.view].x < 0 || [gestureRecognizer translationInView:gestureRecognizer.view].y > 0 || [gestureRecognizer translationInView:gestureRecognizer.view].y < 0) {
        
        return NO;
    }
    //判断是否此VC是根视图控制VC还有就是动画正在执行
    return self.viewControllers.count != 1 && ![[self valueForKey:@"_isTransitioning"] boolValue];
    
}

@end
